sfslite_file=sfslite-0.8.16.tar.gz
sfslite_url=http://dist.okws.org/dist/$(sfslite_file)
sfslite_dir=sfslite-0.8.16

#chord_file=chord-0.1-20070529.tar.bz2
chord_file=chord-0.1-20071115.tar.bz2
chord_url=http://pdos.csail.mit.edu/chord/snapshots/$(chord_file)
chord_dir=chord-0.1

#Wymagane pakiety: libdb4.4-dev pkg-config g++ libtool automake1.9 autoconf patch bzip2 make automake1.9 autoconf autoconf-archive g++ libtool libgmp3-dev


build_all: build_sfslite build_chord

build_chord: copy_code patch_chord
	mkdir -p build/chord
	cd build/chord && ../../$(chord_dir)/configure --with-sfs=../sfslite
	$(MAKE) -C build/chord
	touch build_chord

build_sfslite: $(sfslite_dir)
	mkdir -p build/sfslite
	cd build/sfslite && ../../$(sfslite_dir)/configure --with-sfsmisc && make
	touch build_sfslite

copy_code: $(chord_dir)
	test -L $(chord_dir)/incognito || ln -s ../incognito $(chord_dir)/incognito

#	test -d $(chord_dir)/incognito || cp -va incognito $(chord_dir)/incognito

patch_chord: $(chord_dir) copy_code
	cd $(chord_dir) && for p in ../chord_*.patch; do patch -p0 < "$$p"; done
	cd $(chord_dir) && autoreconf -f -i
	
unpack: $(chord_dir) $(sfslite_dir)
download: $(chord_file) $(sfslite_file)

$(chord_file):
	wget $(chord_url)
$(sfslite_file):
	wget $(sfslite_url)

$(chord_dir): $(chord_file)
	tar jxf $(chord_file)
	touch $(chord_dir)
$(sfslite_dir): $(sfslite_file)
	tar zxf $(sfslite_file)
	touch $(sfslite_dir)

doxydoc:
	doxygen Doxyfile
clean:
	rm -fr $(chord_dir)
	rm -fr $(sfslite_dir)
	rm -fr build build_chord build_sfslite
clean-chord:
	rm -fr $(chord_dir)
	rm -fr build/chord build_chord
clean-all: clean
	rm -fr $(chord_file)
	rm -fr $(sfslite_file)

dist:
	rm -fr dist
	mkdir -p dist/lsd
	mkdir -p dist/maint
	cp build/chord/lsd/adbd dist/lsd/
	cp build/chord/lsd/lsd dist/lsd/
	cp build/chord/maint/maintd dist/maint/
	cp build/chord/lsd/lsdctl dist/lsd/
	cp $(chord_dir)/lsd/start-dhash dist/
#	find dist -type f | xargs strip

HOSTS=1 2 3 4 5 6
NODES=1 2 3

send_dist:
	for i in $(HOSTS); do\
		tar cf - dist | ssh chordtest@test$$i.student.pw.edu.pl '(cd /tmp; pwd; rm -fr dist; tar xvf -)'; \
	done

stop_nodes:
	for i in $(HOSTS); do \
		ssh chordtest@test$$i.student.pw.edu.pl 'killall lsd;killall maintd;killall adbd'; \
	done
clean_nodes:
	for i in $(HOSTS); do \
		ssh chordtest@test$$i.student.pw.edu.pl 'rm -fr /tmp/dhash-`hostname`-*'; \
	done

start_bootstrap:
	rm -fr /tmp/dist
	cp -a dist /tmp/dist
	cd /tmp/dist && ./start-dhash --root /tmp/dhash-bootstrap -p 10000 -j test0:10000

start_nodes:
	for i in $(HOSTS); do \
		ssh chordtest@test$$i.student.pw.edu.pl '(cd /tmp/dist; ./start-dhash --daemonize --root /tmp/dhash-`hostname`-root -j test0:10000 -p 10000)'; \
		for p in $(NODES); do \
			ssh chordtest@test$$i.student.pw.edu.pl "(cd /tmp/dist; ./start-dhash --daemonize --root /tmp/dhash-\`hostname\`-$$p -j \`hostname\`:10000 -p 12"$$p"34)"; \
		done \
	done
